﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla;
using Csla.Serialization;
using log4net;

namespace Sireass.Bl
{
	[Serializable]
	public class NombreValorTinyList : NameValueListBase<byte, string>
	{
		// Create a logger for use in this class
		private static readonly ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

		public static void GetNombreValorTinyList(SingleCriteria<string> tabla, EventHandler<DataPortalResult<NombreValorTinyList>> callback)
		{
			DataPortal.BeginFetch<NombreValorTinyList>(tabla, callback);
		}

		public static NombreValorTinyList GetNombreValorTinyList(SingleCriteria<string> tabla)
		{
			return DataPortal.Fetch<NombreValorTinyList>(tabla);
		}

		private void DataPortal_Fetch(SingleCriteria<string> tabla)
		{
			if (log.IsDebugEnabled)
			{
				log.Debug("tabla.value: " + tabla.Value);
			}
			using (var dalManager = Sireass.Dal.DalFactory.GetManager())
			{
				var dal = dalManager.GetProvider<Sireass.Dal.INombreValorDal>();
				
				using (var data = dal.Fetch(tabla.Value))
				{
					IsReadOnly = false;
					var rlce = RaiseListChangedEvents;
					RaiseListChangedEvents = false;
					while (data.Read())
					{
						Add(new NameValuePair(
						  data.GetByte(0), data.GetString(1)));
					}
					RaiseListChangedEvents = rlce;
					IsReadOnly = true;
				}
			}
		}
	}
}
